//
//  CZStatusViewModel.swift
//  sinaWeiboswift
//
//  Created by 段文杰 on 16/4/13.
//  Copyright © 2016年 段文杰. All rights reserved.
//

import UIKit

class CZStatusViewModel: NSObject {
    // 单例
    static let sharedInstance: CZStatusViewModel = CZStatusViewModel()
    
    /// 加载微博数据
    
    func loadStatus(loadStatusCallback: (statuses: [CZStatusModel]?, error: NSError?)-> ()) {
        // 1.urlString
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        // 2.参数
        let parameters = ["access_token": CZUseraccountViewModel.sharInatance.userAccount!.access_token!]


        CZNetWorkingTool.sharInstance.GET(urlString, parameters: parameters, progress: nil, success: { (task, responseObject) in
        HMPrint("获取微博数据成功了: \(responseObject)")
            // 获取微博数据
            if let dict = responseObject as? [String: AnyObject], let statusesDict = dict["statuses"] as? [[String: AnyObject]] {
                
                // 定义数组来保存转好的微博模型
                //                var statusModels = [HMStatus]()
                var statusModels: [CZStatusModel] = []
                
                // 遍历每个字典
                for dict in statusesDict {
                    // 字典转模型
                    let status = CZStatusModel(dict: dict)
                    
                    // 将模型添加到数组里面
                    statusModels.append(status)
                }
                loadStatusCallback(statuses: statusModels, error: nil)
            }

            }) { (task, error) in
                
                
                

                HMPrint("获取微博数据失败了\(error)")
        }
    }
}
